﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Citi.RoutineBase;

namespace Citi.GenericApplications.SchedulerUi
{
    public class CopyFiles : IExecutableRoutine
    {
        public IEnumerable<RoutineParameter> Parameters
        {
            get
            {
                return new List<RoutineParameter> { 
                    new RoutineParameter{ Description = "Files to copy. Separator: pipe '|'", Name = "FilesToCopy", Nullable = false, Required = true, Type = typeof(string)},
                    new RoutineParameter{ Description = "Destination Files or Path (filename will be kept). Separator: pipe '|' ", Name ="Destination", Nullable = false, Required = true, Type = typeof(string)},
                    new RoutineParameter{ Description = "If you want to start a routine when it's finished, insert the Routine Id. Multiples routines: Separator pipe '|'", 
                        Name ="SuccessorRoutineId", Nullable = true, Required = false, Type = typeof(string)}
                };
            }
        }

        public void Rollback(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            
        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            var filesToCopy =  parameters["FilesToCopy"].ToString().Split('|').ToList();
            var destination = parameters["Destination"].ToString().Split('|').ToList();
            var successorRoutineId = parameters.ContainsKey("SuccessorRoutineId") ? parameters["SuccessorRoutineId"].ToString() : null;

            currentAction.Description = "Validating data";

            if (destination.Count() > 1 && filesToCopy.Count() != destination.Count())
                throw new Exception("Numbers of files to copy and destination must be the same");

            if (destination.Count() == 1 && filesToCopy.Count() > 1 && !Directory.Exists(destination.First()))
                throw new Exception("When there're more than 1 file to copy and only one destination, the destination path must be a folder");

            foreach (var file in filesToCopy)
            {
                var destinationFile = destination.Count() > 1 ? destination[filesToCopy.IndexOf(file)] : destination.First();
                currentAction.Description = string.Format("Copying '{0} to '{1}", file, destinationFile);

                if(Directory.Exists(file))
                {
                    currentAction.Description = "Copy entire folder is not supported";
                    continue;
                }

                if (!File.Exists(file))
                {
                    currentAction.Description = "File does not exist";
                    continue;
                }

                if (Directory.Exists(destinationFile))
                    destinationFile = Path.Combine(destinationFile, Path.GetFileName(file));

                File.Copy(file, destinationFile, true);
                currentAction.Description = "Copied";
            }

            Util.ScheduleSuccessorRoutine(successorRoutineId, currentAction);
        }

        public void Stop(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            
        }
    }
}
